import SelectAppList from '@/components/select-app-list';

export default {
    name: 'operation-operational-activities-activity-management',
    components: {
        SelectAppList,
    },
    data() {
        const checkLimitAmount = (rule, value, callback) => {
            if (value < 300) {
                callback(new Error(this.$tc('verify.mustGreaterThan', 1, { num: 300 }))); // '不小于300'
            } else {
                callback();
            }
        };
        return {
            searchForm: {
                appName: '',
            },
            searchFormRules: {
                appName: [],
            },
            form: {
                id: '',
                limitAmount: '', // 最低提现金额
                registerAmount: '', // 注册发送金额
                registerAmountButton: '0', // 注册发送金额开关 0关 1开
                inviteLoanAmount: '', //申请借款节点邀请人获得奖励金额
                inviteLoanAmountButton: '0', //申请借款节点邀请人开关 0关 1开
                inviteLoanSuccessAmount: '', //放款成功节点邀请人获得奖励金额
                inviteLoanSuccessButton: '0', //放款成功节点邀请人开关 0关 1开
                inviteRepaySuccessAmount: '', //还款成功节点邀请人获得奖励金额
                inviteRepaySuccessButton: '0', //还款成功节点邀请人开关 0关 1开
                inviteesLoanAmount: '', //申请借款节点被邀请人获得奖励金额
                inviteesLoanAmountButton: '0', //申请借款节点被邀请人开关 0关 1开
                inviteesLoanSuccessAmount: '', //放款成功节点被邀请人获得奖励金额
                inviteesLoanSuccessButton: '0', //放款成功节点被邀请人开关 0关 1开
                inviteesRepaySuccessAmount: '', //还款成功节点被邀请人获得奖励金额
                inviteesRepaySuccessButton: '0', //还款成功节点被邀请人开关 0关 1开
            },
            rules: {
                limitAmount: [
                    { required: true, message: this.$tc('verify.plsInputInteger') }, // 请输入正整数
                    { pattern: /^[1-9]\d*$/, message: this.$tc('verify.plsInputInteger') }, // 请输入正整数
                    { validator: checkLimitAmount }, // 不小于300
                ], // 最低提现金额
                registerAmount: [
                    { required: true, message: this.$tc('verify.plsInputInteger') }, // 请输入正整数
                    { pattern: /^[1-9]\d*$/, message: this.$tc('verify.plsInputInteger') }, // 请输入正整数
                ], // 注册发送金额
                inviteLoanAmount: [
                    { required: true, message: this.$tc('verify.plsInputInteger') }, // 请输入正整数
                    { pattern: /^[1-9]\d*$/, message: this.$tc('verify.plsInputInteger') }, // 请输入正整数
                ], //申请借款节点邀请人获得奖励金额
                inviteLoanSuccessAmount: [
                    { required: true, message: this.$tc('verify.plsInputInteger') }, // 请输入正整数
                    { pattern: /^[1-9]\d*$/, message: this.$tc('verify.plsInputInteger') }, // 请输入正整数
                ], //放款成功节点邀请人获得奖励金额
                inviteRepaySuccessAmount: [
                    { required: true, message: this.$tc('verify.plsInputInteger') }, // 请输入正整数
                    { pattern: /^[1-9]\d*$/, message: this.$tc('verify.plsInputInteger') }, // 请输入正整数
                ], //还款成功节点邀请人获得奖励金额
                inviteesLoanAmount: [
                    { required: true, message: this.$tc('verify.plsInputInteger') }, // 请输入正整数
                    { pattern: /^[1-9]\d*$/, message: this.$tc('verify.plsInputInteger') }, // 请输入正整数
                ], //申请借款节点被邀请人获得奖励金额
                inviteesLoanSuccessAmount: [
                    { required: true, message: this.$tc('verify.plsInputInteger') }, // 请输入正整数
                    { pattern: /^[1-9]\d*$/, message: this.$tc('verify.plsInputInteger') }, // 请输入正整数
                ], //放款成功节点被邀请人获得奖励金额
                inviteesRepaySuccessAmount: [
                    { required: true, message: this.$tc('verify.plsInputInteger') }, // 请输入正整数
                    { pattern: /^[1-9]\d*$/, message: this.$tc('verify.plsInputInteger') }, // 请输入正整数
                ], //还款成功节点被邀请人获得奖励金额
            },
        };
    },
    watch: {
        form: {
            deep: true,
            immediate: true,
            handler(value) {
                const {
                    registerAmountButton, inviteLoanAmountButton, inviteLoanSuccessButton, inviteRepaySuccessButton,
                    inviteesLoanAmountButton, inviteesLoanSuccessButton, inviteesRepaySuccessButton,
                } = value;
                if (this.isClosed(registerAmountButton)) {
                    this.form.registerAmount = 0;
                }
                if (this.isClosed(inviteLoanAmountButton)) {
                    this.form.inviteLoanAmount = 0;
                }
                if (this.isClosed(inviteLoanSuccessButton)) {
                    this.form.inviteLoanSuccessAmount = 0;
                }
                if (this.isClosed(inviteRepaySuccessButton)) {
                    this.form.inviteRepaySuccessAmount = 0;
                }
                if (this.isClosed(inviteesLoanAmountButton)) {
                    this.form.inviteesLoanAmount = 0;
                }
                if (this.isClosed(inviteesLoanSuccessButton)) {
                    this.form.inviteesLoanSuccessAmount = 0;
                }
                if (this.isClosed(inviteesRepaySuccessButton)) {
                    this.form.inviteesRepaySuccessAmount = 0;
                }
            },
        },
    },
    methods: {
        isClosed(status) {
            return status === '0';
        },
        // 获取配置信息
        getConfig() {
            const { appName } = this.searchForm;
            if (!appName) {
                return this.$message.warning(this.$tc('verify.plsSelectAPP')); // 请选择APP
            }
            this.$api.operation.fission.getFissionConfig({ appName }).then(res => {
                const { status, data, error } = res;
                if ('1' === status && '00000000' === error) {
                    const form = this.form;
                    Object.keys(form).forEach(key => {
                        form[key] = data[key];
                    });
                }
            });
        },
        // app列表
        appData(list) {
            if (list) {
                const domain = this.$storage.get('user').domain;
                if ('Pesopop' === domain) {
                    list.forEach(item => {
                        if (item.name === 'PesoHere') {
                            // 默认选中PesoHere
                            this.searchForm.appName = 'PesoHere';
                        }
                    });
                } else {
                    const app = list[0] || {};
                    this.searchForm.appName = app.name || '';
                }
                // this.getConfig();
            }
        },
        // 保存配置
        save() {
            this.$refs.configForm.validate(value => {
                if (value) {
                    const params = Object.assign({}, this.form);
                    this.$api.operation.fission.updateFissionConfig(params).then(res => {
                        const { status, error } = res;
                        if ('1' === status && '00000000' === error) {
                            this.$message.success(this.$tc('common.saveSuccess')); // 保存成功
                        }
                    });
                }
            });
        },
    },
    created() {
        //
    },
};
